* Required package - rdrobust
*net install st0366_1.pkg

* Required package - frmttable
*net install sg97_5.pkg

* Restricted relication data
u restricteddata, clear

	
*********************************************************************************
       **************************** TABLES *****************************
*********************************************************************************

********************************************
* Table 1
********************************************

* List of other covariates
global vars female age attempts prepprogram private_hs low20 high80 /// 
	parents_heduc parents_unemp parents_entrep capital prog_market ///
	prog_quality self_full prog_other tuition_fees univ_prestige other_reasons


* Stats for admitted and non-admitted applicants
qui forv f=0/1 {
	mat S`f' = J(1,2,.)
	mat coln S`f' = "Mean `g'`f'" "Std dev `g'`f'"
	
	* Stats per variable
	foreach v in crime_adm10 std_admscore enrolled attendance attainment ///
		gradufpes employed_adm10 lsalary_adm10 migration_adm10  ///
		$vars { 
	
		sum `v' if admission==`f' 
		mat mean = round(r(mean),.0001)
		mat sd = round(r(sd),.0001)
		mat rown mean = `v'
		mat S`f' = S`f' \  mean, sd
	} 
}

mat Sall = J(1,2,.)
mat coln Sall = "All"

* Stats for all applicants
qui foreach v of varlist crime_adm10 std_admscore enrolled attendance ///
	attainment gradufpes employed_adm10 lsalary_adm10 migration_adm10  ///
	$vars { 
	
	sum `v'
	mat meanall = round(r(mean),.0001)
	mat sdall = round(r(sd),.0001)			
	mat rown meanall = `v'
	mat Sall = Sall \ meanall, sdall
} 

mat sum = Sall, S1, S0 
mat sum = sum[2...,1...]


* Final table
frmttable, replace statmat(sum) varlabels substat(0) sdec(3) tex



********************************************
* Table 2
********************************************

* Bandwidth variable
cap g kw = .

* Matrices storing estimates
mat B = J(1,32,.) // RD estimates + substats
mat P = J(1,32,.) // significance


* Estimation per income group
qui foreach g in all low20 high80 { 

	* Matrices storing estimates
	mat A = J(2,1,.) // RD estimates + substats
	mat M = J(2,1,.) // significance


	* Estimation per type of crime
	foreach y in crime_adm10 crime_violent_adm10 crime_property_adm10 ///
		crime_drugs_adm10 crime_public_adm10 crime_public_drug_adm10  ///
		crime_traffic_adm10 crime_unclassified_adm10 {
	
		* Optimal bandwidth
		rdbwselect `y' std_admscore if `g'==1, vce(cluster id)
		loc bw = e(h_mserd)
		replace kw = max(0,1 - abs(std_admscore)/`bw')
		
		* Baseline rates
		reg `y' if std_admscore<=0 & `g'==1 [aw=kw] 
		mat mean = round(_b[_cons],.001)
			
		* SRD estimate
		rdrobust `y' std_admscore if `g'==1, vce(cluster id)
		mat B1 = round(e(tau_bc),.001) , round(e(se_tau_rb),.001) , mean,  e(N)
		loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2,cond(e(pv_rb)<.1,1,0)))
		mat N1 = `star' , 0 , 0 , 0	
		
		* FRD estimate
		rdrobust `y' std_admscore if `g'==1, vce(cluster id) all fuzzy(enrolled)
		mat B2 = round(e(tau_bc),.001) , round(e(se_tau_rb),.001), mean , e(N) 
		loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2,cond(e(pv_rb)<.1,1,0)))
		mat N2 = `star' , 0 , 0 , 0		

		mat A = A , (B1 \ B2)
		mat M = M , (N1 \ N2)
	}
	
	* Eliminate first column with dots
	mat A = A[1...,2...]
	mat M = M[1...,2...]
	
	* Add estimates per group below major matrix
	mat B = B \ A
	mat P = P \ M
	
}


* Eliminate first row with dots
mat B = B[2...,1....]
mat P = P[2...,1....]


* Final table
frmttable, replace statmat(B) varlabels substat(3) sdec(3 \ 3 \ 3 \ 0) tex ///
    annotate(P) asymbol($^*$,$^{**}$,$^{***}$) ///
	brackets("", "" \ (,) \ "" \ "") ///
	rtitle("Admission"\""\"\textit{Baseline rate}"\"N"\"Enrollment"\""\ ///
	"\textit{Baseline rate}"\"N"\"Admission"\""\"\textit{Baseline rate}"\"N"\ ///
	"Enrollment"\""\"\textit{Baseline rate}"\"N"\"Admission"\""\ ///
	"\textit{Baseline rate}"\"N"\"Enrollment"\""\"\textit{Baseline rate}"\"N") ///
	ctitle("effects" "all" "violent" "property" "drug" "public" "public + drug" ///
	"traffic" "unclassif.")



******************************************
* Table 3
******************************************	

* Bandwidth variable
cap g kw = .


* Outcome
loc y "crime_adm10"


* Estimation per type of program classification

loc j = 0
qui foreach p of varlist lowSESprog_inc lowSESprog_sch {

	* Matrices storing estimates
	mat H`j' = J(1,8,.) // RD estimates + substats
	mat P`j' = J(1,8,.) // significance


	* Estimation per income groups
	foreach z of varlist low20 high80 {
	
		* Matrices storing estimates
		mat A = J(2,1,.) // RD estimates + substats
		mat M = J(2,1,.) // significance


		* Estimation per type of program (below & above median)
		forv i = 0/1 {

			* Optimal bandwidth
			rdbwselect `y' std_admscore if `p'==`i' & `z'==1 & ///
				employed_adm10==1, vce(cluster id)
			loc bw = e(h_mserd)
			replace kw = max(0,1 - abs(std_admscore)/`bw')
			
			* Baseline rate
			reg `y'  if std_admscore<=0 & `p'==`i' & `z'==1 & ///
				employed_adm10==1 [aw=kw] 
			mat mean = round(_b[_cons],.001)		
			
			* SRD estimate
			rdrobust `y' std_admscore if `p'==`i' & `z'==1 & ///
				employed_adm10==1, vce(cluster id) 
			mat B1 = round(e(tau_bc),.001), round(e(se_tau_rb),.001), mean, e(N) 
			loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2, ///
				cond(e(pv_rb)<.1,1,0)))
			mat N1 =  `star' , 0, 0 , 0  
			
			* FRD estimate
			rdrobust `y' std_admscore if `p'==`i' & `z'==1 & ///
				employed_adm10==1, vce(cluster id) all  fuzzy(enrolled) 
			mat B2 = round(e(tau_bc),.001), round(e(se_tau_rb),.001), mean, e(N) 
			loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05, 2, ///
				cond(e(pv_rb)<.1,1,0)))
			mat N2 =  `star' , 0, 0 , 0  
			
			mat A = A , (B1 \ B2)
			mat M = M , (N1 \ N2)	
		}
		
		* Eliminate first column with dots
		mat A = A[1...,2...]
		mat M = M[1...,2...]
	
		* Add estimates per group below major matrix
		mat H`j' = H`j' \ A
		mat P`j' = P`j' \ M
	}

	* Eliminate first row with dots
	mat H`j' = H`j'[2...,1....]
	mat P`j' = P`j'[2...,1....]
	
	loc j = `j' + 1
}

mat B = H0, H1 
mat P = P0, P1


* Final table
frmttable , replace statmat(B) varlabels substat(3) sdec(3 \ 3 \ 3 \ 0) tex ///
    annotate(P) asymbol($^*$,$^{**}$,$^{***}$) ///
	brackets("", "" \ (,) \ "" \ "") ctitle("", High , Low , High , Low) ///
	rtitle("Admission"\""\"\textit{Baseline rate}"\"N"\"Enrollment"\""\ ///
		"\textit{Baseline rate}"\"N"\"Admission"\""\"\textit{Baseline rate}"\ ///
		"N"\"Enrollment"\""\"\textit{Baseline rate}"\"N")



******************************************
* Table 4
******************************************

* Bandwidth variable
cap g kw = .


* Outcome
loc y "crime_adm10"


* Estimation per income group
loc j = 0
qui foreach v of varlist all low20 high80 {
	
	* Optimal bandwidth
	rdbwselect `y' std_admscore if employed_adm10==1 & `v'==1, ///
		vce(cluster id)
	loc bw = e(h_mserd)
	replace kw = max(0,1 - abs(std_admscore)/`bw')
		
	* Baseline rate
	reg `y' if std_admscore<=0 & employed_adm10==1 & `v'==1 [aw=kw] 
	mat mean = round(_b[_cons],.001)


	* RD estimate - no control
	rdrobust `y' std_admscore if employed_adm10==1 & `v'==1, ///
		vce(cluster id) all  
	mat B1 = round(e(tau_bc),.001) , round(e(se_tau_rb),.001) , mean , e(N) 
	loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2,cond(e(pv_rb)<.1,1,0)))
	mat N1 = `star' , 0 , 0 , 0		


	* RD estimate - controlling for college attendance
	rdrobust `y' std_admscore if employed_adm10==1 & `v'==1 , ///
		vce(cluster id) all covs(attendance) 
	mat B2 = round(e(tau_bc),.001) , round(e(se_tau_rb),.001) , mean , e(N) 
	loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2,cond(e(pv_rb)<.1,1,0)))
	mat N2 = `star' , 0 , 0 , 0		


	* RD estimate - controlling for college attainment
	rdrobust `y' std_admscore if employed_adm10==1 & `v'==1 , ///
		vce(cluster id) all covs(attainment) 
	mat B3 = round(e(tau_bc),.001) , round(e(se_tau_rb),.001) , mean , e(N) 
	loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2,cond(e(pv_rb)<.1,1,0)))
	mat N3 = `star' , 0 , 0 , 0		

	
	* RD estimate - controlling for attendance and attainment
	rdrobust `y' std_admscore if employed_adm10==1 & `v'==1, ///
		vce(cluster id) all covs(attendance attainment)
	mat B4 = round(e(tau_bc),.001) , round(e(se_tau_rb),.001) , mean , e(N) 
	loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2,cond(e(pv_rb)<.1,1,0)))
	mat N4 = `star' , 0 , 0 , 0		
	
	* Matrices storing estimates
	mat H`j' = B1, B2, B3, B4 // RD estimates + substats
	mat M`j' = N1, N2, N3, N4 // significance
	
	loc j = `j' + 1
}

mat B = H0 \ H1 \ H2 
mat P = M0 \ M1 \ M2 


* Final table
frmttable , replace statmat(B) varlabels substat(3) sdec(3 \ 3 \ 3 \ 0) tex ///
    annotate(P) asymbol($^*$,$^{**}$,$^{***}$) ///
	brackets("", "" \ (,) \ "" \ "") ///
	rtitle("Admission"\""\"\textit{Baseline mean}"\"N"\"Admission"\""\ ///
		"\textit{Baseline mean}"\"N"\"Admission"\""\"\textit{Baseline mean}"\"N")



******************************************
* Table 5
******************************************	

* Bandwidth variable
cap g kw = .


* Outcome
loc y "attendance"


* Estimation per type of program classification

loc j = 0
qui foreach p of varlist lowSESprog_inc lowSESprog_sch {

	* Matrices storing estimates
	mat H`j' = J(1,8,.) // RD estimates + substats
	mat P`j' = J(1,8,.) // significance


	* Estimation per income groups
	foreach z of varlist low20 high80 {
	
		* Matrices storing estimates
		mat A = J(2,1,.) // RD estimates + substats
		mat M = J(2,1,.) // significance


		* Estimation per type of program (below & above median)
		forv i = 0/1 {

			* Optimal bandwidth
			rdbwselect `y' std_admscore if `p'==`i' & `z'==1 & ///
				employed_adm10==1, vce(cluster id)
			loc bw = e(h_mserd)
			replace kw = max(0,1 - abs(std_admscore)/`bw')
			
			* Baseline rate
			reg `y'  if std_admscore<=0 & `p'==`i' & `z'==1 & ///
				employed_adm10==1 [aw=kw] 
			mat mean = round(_b[_cons],.001)		
			
			* SRD estimate
			rdrobust `y' std_admscore if `p'==`i' & `z'==1 & ///
				employed_adm10==1, vce(cluster id) 
			mat B1 = round(e(tau_bc),.001), round(e(se_tau_rb),.001), mean, e(N) 
			loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2, ///
				cond(e(pv_rb)<.1,1,0)))
			mat N1 =  `star' , 0, 0 , 0  
			
			* FRD estimate
			rdrobust `y' std_admscore if `p'==`i' & `z'==1 & ///
				employed_adm10==1, vce(cluster id) all  fuzzy(enrolled) 
			mat B2 = round(e(tau_bc),.001), round(e(se_tau_rb),.001), mean, e(N) 
			loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2, ///
				cond(e(pv_rb)<.1,1,0)))
			mat N2 =  `star' , 0, 0 , 0  
			
			mat A = A , (B1 \ B2)
			mat M = M , (N1 \ N2)	
		}
		
		* Eliminate first column with dots
		mat A = A[1...,2...]
		mat M = M[1...,2...]
	
		* Add estimates per group below major matrix
		mat H`j' = H`j' \ A
		mat P`j' = P`j' \ M
	}

	* Eliminate first row with dots
	mat H`j' = H`j'[2...,1....]
	mat P`j' = P`j'[2...,1....]
	
	loc j = `j' + 1
}

mat B = H0, H1 
mat P = P0, P1


* Final table
frmttable , replace statmat(B) varlabels substat(3) sdec(3 \ 3 \ 3 \ 0) tex ///
    annotate(P) asymbol($^*$,$^{**}$,$^{***}$) ///
	brackets("", "" \ (,) \ "" \ "") ctitle("", High , Low , High , Low) ///
	rtitle("Admission"\""\"\textit{Baseline rate}"\"N"\"Enrollment"\""\ ///
		"\textit{Baseline rate}"\"N"\"Admission"\""\"\textit{Baseline rate}"\ ///
		"N"\"Enrollment"\""\"\textit{Baseline rate}"\"N")



******************************************
* Table 6
******************************************	

* Bandwidth variable
cap g kw = .


* Outcome
loc y "hq_degree"


* Estimation for all sample and for those with college degree
loc z = 1
qui foreach l in all attainment {

	* Matrices storing estimates
	mat H`z' = J(1,12,.) // RD estimates + substats
	mat P`z' = J(1,12,.) // significance

	* Estimation per income groups
	foreach g in all low20 high80 { 
	
		* Estimation per type of program (below & above median)
		foreach p in all lowSESprog_inc {
	
			if "`p'"=="all" {
			
				* Optimal bandwidth
				rdbwselect `y' std_admscore if `g'==1 & `l'==1, ///
					vce(cluster id)
				loc bw = e(h_mserd)
				replace kw = max(0,1 - abs(std_admscore)/`bw')
			
				* Baseline rate
				reg `y'  if std_admscore<=0 & `g'==1 & `l'==1 [aw=kw] 
				mat mean = round(_b[_cons],.001)
				
				* SRD estimate
				rdrobust `y' std_admscore if `g'==1 & `l'==1, ///
					vce(cluster id) 
				mat A = round(e(tau_bc),.001) , round(e(se_tau_rb),.001) , ///
					mean,  e(N)
				loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2, ///
					cond(e(pv_rb)<.1,1,0)))
				mat M = `star' , 0 , 0 , 0	
			
				* FRD estimate
				rdrobust `y' std_admscore if `g'==1 & `l'==1, ///
					vce(cluster id) all fuzzy(enrolled)
				mat B = round(e(tau_bc),.001) , round(e(se_tau_rb),.001), ///
					mean , e(N)
				loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2, ///
					cond(e(pv_rb)<.1,1,0)))
				mat N = `star' , 0 , 0 , 0
				
				mat A = A \ B
				mat M = M \ N
			}
			else {
				forv i = 0/1 {
					
					* Optimal bandwidth
					rdbwselect `y' std_admscore if `g'==1 & `l'==1 & ///
						`p'==`i', vce(cluster id)
					loc bw = e(h_mserd)
					replace kw = max(0,1 - abs(std_admscore)/`bw')
				
					* Baseline rate
					reg `y'  if std_admscore<=0 & `g'==1 & `l'==1 & ///
						`p'==`i' [aw=kw] 
					mat mean = round(_b[_cons],.001)
					
					* SRD estimate
					rdrobust `y' std_admscore if `g'==1 & `l'==1 & ///
						`p'==`i', vce(cluster id) 
					mat B1 = round(e(tau_bc),.001), round(e(se_tau_rb),.001), ///
						mean,  e(N)
					loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2, ///
						cond(e(pv_rb)<.1,1,0)))
					mat N1 = `star' , 0 , 0 , 0	
				
					* FRD estimate
					rdrobust `y' std_admscore if `g'==1 & `l'==1 & ///
						`p'==`i', vce(cluster id) all  fuzzy(enrolled)
					mat B2 = round(e(tau_bc),.001), round(e(se_tau_rb),.001), ///
						mean , e(N)
					loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2, ///
						cond(e(pv_rb)<.1,1,0)))
					mat N2 = `star' , 0 , 0 , 0
					
					mat A = A , (B1 \ B2)
					mat M = M , (N1 \ N2)
				}
			}	
		}
		
		mat H`z' = H`z' \ A
		mat P`z' = P`z' \ M
	}

	* Eliminate first row with dots
	mat H`z' = H`z'[2...,1...]
	mat P`z' = P`z'[2...,1...]
	
	loc z = `z'+1
}

mat H = H1 , H2
mat P = P1 , P2


* Final table
frmttable , replace statmat(H) varlabels substat(3) sdec(3 \ 3 \ 3 \ 0) tex ///
    annotate(P) asymbol($^*$,$^{**}$,$^{***}$) ///
	brackets("", "" \ (,) \ "" \ "") ///
	ctitle("","All","High","Low","All","High","Low") ///
	rtitle("Admission"\""\"\textit{Baseline rate}"\"N"\"Enrollment"\""\ ///
		"\textit{Baseline rate}"\"N"\"Admission"\""\ ///
		"\textit{Baseline rate}"\"N"\"Enrollment"\""\ ///
		"\textit{Baseline rate}"\"N"\"Admission"\""\ ///
		"\textit{Baseline rate}"\"N"\"Enrollment"\""\ ///
		"\textit{Baseline rate}"\"N")



******************************************
* Table 7
******************************************

* Bandwidth variable
cap g kw = .


* Estimation per income group
loc z = 1
qui foreach g of varlist all low20 high80 { 
	
	* Matrices storing estimates
	mat H`z' = J(1,1,.) // RD estimates + substats
	mat M`z' = J(1,1,.) // significance

	
	* Estimation per outcome
	foreach y in divorce_adm10 nopay_adm10 civil_other_adm10 {

		* Optimal bandwidth
		rdbwselect `y' std_admscore if `g'==1, vce(cluster id)
		loc bw = e(h_mserd)
		replace kw = max(0,1 - abs(std_admscore)/`bw')
		
		* Baseline rate
		reg `y'  if std_admscore<=0 & `g'==1 [aw=kw] 
		mat mean = round(_b[_cons],.001)
			
		* SRD estimate
		rdrobust `y' std_admscore if `g'==1, vce(cluster id) 
		mat A = round(e(tau_bc),.001) , round(e(se_tau_rb),.001) , mean,  e(N) 
		loc star = cond(e(pv_rb)<=.01,3,cond(e(pv_rb)<=.05,2, ///
			cond(e(pv_rb)<.1,1,0)))
		mat M`z' = `star' , 0 , 0 , 0	

		mat H`z' = H`z', A
		mat M`z' = M`z', M`z'
	}
	
	* Eliminate first column with dots
	mat H`z' = H`z'[1...,2...]
	mat M`z' = M`z'[1...,2...]
	
	loc z = `z' + 1
}

mat H = H1 \ H2 \ H3
mat P = M1 \ M2 \ M3


* Final table
frmttable , replace statmat(H) varlabels substat(3) sdec(3 \ 3 \ 3 \ 0) tex ///
    annotate(P) asymbol($^*$ , $^{**}$ , $^{***}$ ) ///
	brackets("", "" \ (,) \ "" \ "") ///
	rtitle("Admission"\""\"\textit{Baseline rate}"\"N"\"Admission"\""\ ///
		"\textit{Baseline rate}"\"N"\"Admission"\""\"\textit{Baseline rate}"\"N")

			

******************************************
* Table 8
******************************************

preserve
	do "backofenvelope.do"
restore


*******************************************************************************
* End of Do file
*******************************************************************************
